//
//  QSShareHelper.m
//  QSSmarkPark-iOS
//
//  Created by 孙俊 on 2018/3/14.
//  Copyright © 2018年 QiSheng. All rights reserved.
//

#import "QSShareHelper.h"
#import "QSShareView.h"

@implementation QSShareHelper

+ (instancetype)sharedHelper {
    static id sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}

- (void)shareWebPageWithTitle:(NSString *)title
                          des:(NSString *)des
                          url:(NSString *)url
                     imageURL:(NSString *)imageURL
                   completion:(UMSocialRequestCompletionHandler)completion {
    [QSShareView showShareMenuViewAndPlatformSelectionBlock:^(UMSocialPlatformType platformType) {
        [self shareToPlatformType:platformType
                         andTitle:title
                              des:des
                            image:nil
                         imageURL:imageURL
                              url:url
                       completion:completion];
    }];
}

- (void)shareImage:(UIImage *)image
        completion:(UMSocialRequestCompletionHandler)completion {
    [QSShareView showShareMenuViewAndPlatformSelectionBlock:^(UMSocialPlatformType platformType) {
        [self shareToPlatformType:platformType
                         andTitle:nil
                              des:nil
                            image:image
                         imageURL:nil
                              url:nil
                       completion:completion];
    }];
}

- (void)shareToPlatformType:(UMSocialPlatformType)platformType
                   andTitle:(NSString *)title
                        des:(NSString *)des
                      image:(UIImage *)image
                   imageURL:(NSString *)imageURL
                        url:(NSString *)url
                 completion:(UMSocialRequestCompletionHandler)completion {
    
    if (platformType == UMSocialPlatformType_WechatSession
        || platformType == UMSocialPlatformType_WechatTimeLine) {
        if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession]) {
            [QSAppKeyWindow showAutoHideHudWithText:@"您还未安装微信"];
            return;
        }
    }
    if (platformType == UMSocialPlatformType_Qzone
        || platformType == UMSocialPlatformType_QQ) {
        if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ]) {
            [QSAppKeyWindow showAutoHideHudWithText:@"您还未安装QQ"];
            return;
        }
    }
    if (platformType == UMSocialPlatformType_Sina) {
        if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_Sina]) {
            [QSAppKeyWindow showAutoHideHudWithText:@"您还未安装新浪微博"];
            return;
        }
    }
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    if (!title
        &&!des
        &&!url
        &&image) {
        //创建图片分享对象
        UMShareImageObject *shareImageObject = [[UMShareImageObject alloc] init];
        shareImageObject.shareImage = image;
        messageObject.shareObject = shareImageObject;
    } else {
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:des thumImage:[UIImage imageNamed:@"share_logo"]];
        //设置网页地址
        shareObject.webpageUrl = url;
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
    }
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
        if (error) {
            DLog(@"************Share fail with error %@*********",error);
        } else {
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                DLog(@"response message is %@",resp.message);
                //第三方原始返回的数据
                DLog(@"response originalResponse data is %@",resp.originalResponse);
                
            } else {
                DLog(@"response data is %@",data);
            }
        }
        //回调
        if (completion) {
            completion(data,error);
        }
    }];
}

- (void)share {
    
}

@end
